package w;

import java.io.File;
import java.util.Vector;

import javax.swing.Icon;
import javax.swing.filechooser.FileSystemView;

/**
 * 
 * A data model for a JTree. This model explorer windows file system directly.
 * 
 * 
 * 
 * <p>
 * 
 * Perhaps there is a fatal bug with this design. For speed, each of instances
 * 
 * of this model contains file objects of subdirectory, up to now, there isn't
 * 
 * any method to release them until program be end. I'm afraid that the memory
 * 
 * would be full of if the file system is large enough and JVM memery size
 * 
 * setted too small.
 * 
 * 
 * 
 * <p>
 * 
 * I won't pay more attention to solve it. it isn't goal of current a exercise.
 * 
 * 
 * 
 * @author Jason
 */

class FolderNode implements I_fileSystem {

	// private static FolderNode theRoot;

	private static FileSystemView fsView;

	private static boolean showHiden = true;;

	private File theFile;

	private Vector<File> all = new Vector<File>();

	private Vector<File> folder = new Vector<File>();

	/**
	 * 
	 * set that whether apply hiden file.
	 * 
	 * 
	 * 
	 * @param ifshow
	 */

	public void setShowHiden(boolean ifshow) {

		showHiden = ifshow;

	}

	public Icon getIcon() {

		return fsView.getSystemIcon(theFile);

	}

	public String toString() {

		// return fsView.

		return fsView.getSystemDisplayName(theFile);

	}

	/**
	 * 
	 * create a root node. by default, it should be the DeskTop in window file
	 * 
	 * system.
	 * 
	 * 
	 */

	public FolderNode() {

		fsView = FileSystemView.getFileSystemView();

		theFile = fsView.getHomeDirectory();

		prepareChildren();

	}

	private void prepareChildren() {

		File[] files = fsView.getFiles(theFile, showHiden);

		for (int i = 0; i < files.length; i++) {

			all.add(files[i]);

			if (files[i].isDirectory()

			&& !files[i].toString().toLowerCase().endsWith(".lnk")) {

				folder.add(files[i]);

			}

		}

	}

	private FolderNode(File file) {

		theFile = file;

		prepareChildren();

	}

	public FolderNode getChild(char fileType, int index) {

		if (I_fileSystem.DIRECTORY == fileType) {

			return new FolderNode(folder.get(index));

		} else if (I_fileSystem.ALL == fileType) {

			return new FolderNode(all.get(index));

		} else if (I_fileSystem.FILE == fileType) {

			return null;

		} else {

			return null;

		}

	}

	public int getChildCount(char fileType) {

		if (I_fileSystem.DIRECTORY == fileType) {

			return folder.size();

		} else if (I_fileSystem.ALL == fileType) {

			return all.size();

		} else if (I_fileSystem.FILE == fileType) {

			return -1;

		} else {

			return -1;

		}

	}

	public boolean isLeaf(char fileType) {

		if (I_fileSystem.DIRECTORY == fileType) {

			return folder.size() == 0;

		} else if (I_fileSystem.ALL == fileType) {

			return all.size() == 0;

		} else if (I_fileSystem.FILE == fileType) {

			return true;

		} else {

			return true;

		}

	}

	public int getIndexOfChild(char fileType, Object child) {

		if (child instanceof FolderNode) {

			if (I_fileSystem.DIRECTORY == fileType) {

				return folder.indexOf(((FolderNode) child).theFile);

			} else if (I_fileSystem.ALL == fileType) {

				return all.indexOf(((FolderNode) child).theFile);

			} else if (I_fileSystem.FILE == fileType) {

				return -1;

			} else {

				return -1;

			}

		} else {

			return -1;

		}

	}

}